package xyz.xtt.schedule.blockchain;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import xyz.xtt.schedule.service.*;

/**
 * @author wangqi
 */
@Slf4j
@Component
public class ScheduledTask {
    @Autowired
    OnChainService onChainService;
    @Autowired
    CheckAndRechargeService checkAndRechargeService;
    @Autowired
    SyncBlockService syncBlockService;
    @Autowired
    LuckPhoneService luckPhoneService;
    @Autowired
    LuckPhoneEntitlementDeliverService luckPhoneEntitlementDeliverService;
    @Autowired
    PayOrderService payOrderService;
    @Autowired
    NftVipService nftVipService;
    @Autowired
    UserFundAccountService userFundAccountService;

    // 每隔3秒执行一次任务,包含铸造和转移
    @Scheduled(fixedDelay = 10000)
    public void mintOnChain() {
        onChainService.onChainTask();
    }
    // 查询提交上链的hash，查询链上信息并更新，需要更新tbl_asset_ext和tbl_asset_transaction表
    // 每隔5秒执行一次任务
    @Scheduled(fixedDelay = 10000)
    public void updateChainInfo() {
        onChainService.updateChainStatus();
    }

    // 同步区块数据
    @Scheduled(fixedDelay = 10000)
    public void syncBlock() {
        syncBlockService.executeTask();
    }

    /**
     * 每天凌晨执行一次,同步靓号池
     */
    @Scheduled(cron = "0 30 0 * * ?")
    public void refreshLuckPhone() {
        luckPhoneService.synPhoneData();
    }


    /**
     * 服务启动后3分钟执行一次,同步靓号池
     */
    @Scheduled(initialDelay = 180000, fixedRate = Long.MAX_VALUE)
    public void initRefreshLuckPhone(){
        luckPhoneService.synPhoneData();
    }

    /**
     * 服务启动后3分钟，同步靓号池
     */
    @Scheduled(initialDelay = 180000, fixedRate = Long.MAX_VALUE)
    public void refreshLuckPhoneAfterStartup() {
        luckPhoneService.synPhoneData();
    }

    /**
     * 每隔5分钟检查和更新一次靓号池订单状态
     */
    @Scheduled(fixedDelay = 300000)
    public void updateLuckPhoneOrderStatus(){
        luckPhoneEntitlementDeliverService.updateLuckPhoneOrderStatus();
    }

    /**
     * 每分钟检查和更新一次支付单状态
     */
    @Scheduled(fixedDelay = 60000)
    public void confirmPayOrderStatus(){
        payOrderService.confirmPayOrderStatus();
    }

    /**
     * 每天早晨8点通知会员过期
     */
    @Scheduled(cron = "0 0 8,12 * * ?")
    public void noticeVipExpire() {
        nftVipService.noticeVipExpire();
    }

    /**
     * 检查并结算卖家账户
     */
    @Scheduled(cron = "0 */10 * * * ?")
    public void checkAndSettleSellerAccount() {
        userFundAccountService.checkAndSettleSellerAccount();
    }
}
